<template>
  <el-tabs v-model="activeName" @tab-click="changeTab">
    <el-tab-pane label="按金额" name="user">
      <div ref="month" v-if="monthShow" :class="className" :style="{height:height,width:width,background:background,padding:padding}" />
      <div ref="chart" v-else :class="className" :style="{height:height,width:width,background:background,padding:padding}" />
    </el-tab-pane>
    <el-tab-pane label="按笔数" name="code">
      <div ref="month2" v-if="monthShow" :class="className" :style="{height:height,width:width,background:background,padding:padding}" />
      <div ref="charts" v-else :class="className" :style="{height:height,width:width,background:background,padding:padding}" />
    </el-tab-pane>
  </el-tabs>

</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    background: {
      type: String,
      default: '#fff'
    },
    padding: {
      type: String,
      default: '16px 16px 0'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    monthShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      activeName: 'user'
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
        this.shadowOptions(val)
      }
    },
    monthShow: {
      deep: true,
      handler(val) {
        this.monthShow = val
        this.changeTab();
      }
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    //   this.montChart()
    // })
    if (this.monthShow) {
      this.$nextTick(() => {
        this.montChart()
      })
    } else {
      this.$nextTick(() => {
        this.initChart()
      })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    changeTab() {
      if (this.activeName === 'user') {
        if (this.monthShow) {
          this.$nextTick(() => {
            this.montChart()
          })
        } else {
          this.$nextTick(() => {
            this.initChart()
          })
        }
      } else {
        if (this.monthShow) {
          this.$nextTick(() => {
            this.montChart2()
          })
        } else {
          this.$nextTick(() => {
            this.refChart()
          })
        }
      }
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart, 'macarons')
      this.setOptions(this.chartData)
    },
    refChart() {
      this.chart = echarts.init(this.$refs.charts, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData, accounts, monthData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: monthData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          name: '万元',
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['长款', '短款', '平款']
        },
        series: [{
          name: '长款', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }, {
          name: '短款',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: actualData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }, {
          name: '平款',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#FBCE77',
              lineStyle: {
                color: '#FBCE77',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: accounts,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    },
    montChart() {
      this.chart = echarts.init(this.$refs.month, 'macarons')
      this.shadowOptions(this.chartData)
    },
    montChart2() {
      this.chart = echarts.init(this.$refs.month2, 'macarons')
      this.shadowOptions(this.chartData)
    },
    shadowOptions({ expectedData, actualData, accounts, monthData } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['长款', '短款', '平款']
        },
        xAxis: [
          {
            type: 'category',
            axisTick: { show: false },
            data: monthData
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '长款',
            type: 'bar',
            data: expectedData
          },
          {
            name: '短款',
            type: 'bar',
            data: actualData
          },
          {
            name: '平款',
            type: 'bar',
            data: accounts
          }
        ]
      })
    },
  }
}
</script>
<style lang="scss" scoped>
  .el-tabs {
    /deep/ .el-tabs__header {
      margin: 0;
      background: #fff;
      padding-left: 20px;
    }
    /deep/ .el-tabs__nav-wrap::after{
      height: 1px;
    }
  }
</style>
